package com.easylinkin.linkappapi.lobar.controller;

import com.easylinkin.linkappapi.lobar.dto.screen.HourAnalysisDTO;
import com.easylinkin.linkappapi.lobar.dto.screen.LobarScreenDTO;
import com.easylinkin.linkappapi.lobar.dto.screen.WarningDTO;
import com.easylinkin.linkappapi.lobar.entity.LobarSet;
import com.easylinkin.linkappapi.lobar.entity.UserStatistics;
import com.easylinkin.linkappapi.lobar.service.LobarScreenService;
import com.easylinkin.linkappapi.lobar.service.LobarSetService;
import com.easylinkin.linkappapi.lobar.vo.LobarScreenWarnVo;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/4/27 14:58
 * @Description: 大屏统计
 */
@RestController
@RequestMapping("lobarScreen")
public class LobarScreenController {

  @Autowired
  private LobarScreenService lobarScreenService;
  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;
  @Autowired
  private LobarSetService lobarSetService;

  /**
   * @Description: 人员分析(劳务公司,在场人数,劳务工种)
   * type有值表示需要过滤，取分包单位数据
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("countAnalysis")
  @ApiOperation("人员分析(劳务公司,在场人数,劳务工种)")
  public RestMessage countAnalysis(Integer type) {
    Map<String, Integer> map = lobarScreenService.countAnalysis(type);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * @Description: 人员统计
   * type有值表示需要过滤，取分包单位数据
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("countUser")
  @ApiOperation("根据工种统计人员")
  public RestMessage countUser(Integer type) {
    Map<String, List<LobarScreenDTO>> map = lobarScreenService.countUser(type);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 当前出场人数时间图
   * type有值表示需要过滤，取分包单位数据
   * @param userWorkType 人员类型 1:作业人员 2:管理人员
   */
  @GetMapping("userByTime")
  @ApiOperation("当前出场人数时间图")
  public RestMessage userByTime(Integer type,Integer userWorkType) {
    List<LobarScreenDTO> lobarScreenDTOS = lobarScreenService.userByTime(type,userWorkType);
    return RestBuilders.successBuilder().data(lobarScreenDTOS).build();
  }

  /**
   * 各参建单位出勤率
   * type有值表示需要过滤，取分包单位数据44
   * @param userWorkType 人员类型 1:作业人员 2:管理人员
   */
  @GetMapping("attendance")
  @ApiOperation("各参建单位出勤率")
  public RestMessage attendance(Integer type,Integer userWorkType) {
    List<LobarScreenDTO> lobarScreenDTOS = lobarScreenService.attendance(type,userWorkType);
    return RestBuilders.successBuilder().data(lobarScreenDTOS).build();
  }

  /**
   * 工时分析
   */
  @GetMapping("hourAnalysis")
  @ApiOperation("工时分析")
  public RestMessage hourAnalysis() {
    HourAnalysisDTO hourAnalysisDTO = lobarScreenService.hourAnalysis();
    return RestBuilders.successBuilder().data(hourAnalysisDTO).build();
  }

  /**
   * 风险预警统计
   */
  @GetMapping("warning")
  @ApiOperation("风险预警统计")
  public RestMessage warning() {
    WarningDTO warningDTO = lobarScreenService.warning();
    return RestBuilders.successBuilder().data(warningDTO).build();
  }

  /**
   * 风险预警统计2
   *
   * @param lobarScreenWarnVo 劳务大屏报警入参vo
   * @return 统一出参
   */
  @PostMapping("/warning")
  @ApiOperation("风险预警统计")
  public RestMessage lobarWarning(@RequestBody LobarScreenWarnVo lobarScreenWarnVo) {
    //原get请求没有传参入口，现在有需要传参的场景，修改原get接口没有任何传参，真DT
    WarningDTO warningDTO = lobarScreenService.lobarWarning(lobarScreenWarnVo);
    return RestBuilders.successBuilder().data(warningDTO).build();
  }

  /**
   * 在场人数
   * type有值表示需要过滤，取分包单位数据
   */
  @GetMapping("onNum")
  @ApiOperation("在场人数")
  public RestMessage onNum(Integer type) {
    Map<String, Integer> map = lobarScreenService.onNum(type,null);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 7天出勤率
   * @param userWorkType 人员类型 1:作业人员 2:管理人员
   */
  @GetMapping("sevenAttendance")
  @ApiOperation("在场人数")
  public RestMessage sevenAttendance(Integer userWorkType) {
    List<UserStatistics> userStatistics = lobarScreenService.sevenAttendance(userWorkType);
    return RestBuilders.successBuilder().data(userStatistics).build();
  }

  /**
   * 面板机最近打卡计数和最近打卡人数
   */
  @GetMapping("today/{deviceCode}")
  @ApiOperation("面板机最近打卡计数和最近打卡人数")
  public RestMessage getByDeviceCode(@PathVariable("deviceCode") String deviceCode) {
    Map<String, Object> map = lobarScreenService.getByDeviceCode(deviceCode);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 获取定位方式
   */
  @GetMapping("onType")
  @ApiOperation("获取定位方式")
  public RestMessage onType() {
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    LobarSet lobarSet = lobarSetService.getByTenantId(tenantId);
    return RestBuilders.successBuilder().data(lobarSet).build();
  }
}
